package com.mianyang.zhangsan.controller;


import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.entity.SecFile;
import com.mianyang.zhangsan.service.ISecFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文件接口
 * @author jiangjr
 * @since 2022-10-23
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class SecFileController extends BaseController {
    @Autowired
    private ISecFileService fileService;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file,
                    @RequestParam("fileType") int fileType) {
        fileService.uploadFile(file, fileType);
        return success();
    }

    /**
     * 下载文件
     */
    @GetMapping("/download/{fileId}")
    public R download(@PathVariable("fileId") Long fileId) throws IOException {
        String filePath = fileService.downloadFile(fileId);
        return success("filePath", filePath);
    }

    /**
     *
     */
    @GetMapping("/online/{fileId}")
    public R online(@PathVariable("fileId") Long fileId, HttpServletResponse response) {
        return success();
    }

    /**
     * 修改文件
     */
    @PutMapping("/edit")
    public R editFile(@RequestBody SecFile file) {
        fileService.updateById(file);
        return success();
    }

    /**
     * 删除文件
     */
    @DeleteMapping("/delete/{fileId}")
    public R deleteFile(@PathVariable("fileId") Long fileId) {
        fileService.removeById(fileId);
        return success();
    }
}
